<template>
  <div class="app-container">
    <el-alert
      title="配置第三方模板Id的消息此处配置短信内容无用，仅供参考"
      type="warning"
    />
    <el-form v-show="showSearch" ref="queryForm" class="mt10" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="模板标题" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Code" prop="templateCode">
        <el-input
          v-model="queryParams.templateCode"
          placeholder="请输入模板CODE"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板类型" prop="templateType">
        <el-select v-model="queryParams.templateType" placeholder="请选择模板类型" clearable size="small">
          <el-option label="短信" value="SMS" />
          <el-option label="邮件" value="EMAIL" />
          <el-option label="微信公众号" value="WX_MP" />
          <el-option label="站内信" value="INTERNAL" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sys:msgtemplate:save']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sys:msgtemplate:update']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sys:msgtemplate:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="msgtemplateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="模板标题" align="center" prop="templateName" />
      <el-table-column label="模板CODE" align="center" prop="templateCode" />
      <el-table-column label="模板类型" align="center" prop="templateTypeDesc" />
      <el-table-column label="模板内容" align="center" prop="templateContent" show-overflow-tooltip />
      <el-table-column label="第三方模板Id" align="center" prop="thirdTemplateId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="handleOpenSend(scope.row)"
          >
            发送
          </el-button>
          <el-button
            v-hasPermi="['sys:msgtemplate:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['sys:msgtemplate:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改message对话框 -->
    <el-dialog v-if="open" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="模板标题" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板标题" />
        </el-form-item>
        <el-form-item label="模板Code" prop="templateCode">
          <el-input v-model="form.templateCode" placeholder="请输入模板CODE" />
        </el-form-item>
        <el-form-item label="模板类型" prop="templateType">
          <el-select v-model="form.templateType" placeholder="请选择模板类型">
            <el-option label="短信" value="1" />
            <el-option label="邮件" value="2" />
            <el-option label="微信公众号" value="3" />
            <el-option label="站内信" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="templateContent">
          <el-input v-model="form.templateContent" type="textarea" placeholder="在这里输入文字"
                    :autosize="{ minRows: 8, maxRows: 8 }"
          />
        </el-form-item>
        <el-form-item v-if="['1','3'].includes(form.templateType)" label="第三方模板Id" prop="thirdTemplateId">
          <el-input v-model="form.thirdTemplateId" placeholder="请输入第三方模板Id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">
          取 消
        </el-button>
      </div>
    </el-dialog>

    <!--发送测试-->
    <el-dialog v-if="sendOpen" title="发送消息" :visible.sync="sendOpen" width="800px" append-to-body>
      <el-form ref="sendForm" :model="sendForm" label-width="100px">
        <el-form-item label="接收人" prop="receiver" :rules="[{ required: true, message: '请输入接收人', trigger: 'blur' }]">
          <el-input v-model="sendForm.receiver" placeholder="请输入接收人" />
        </el-form-item>
        <el-form-item label="模板Code" prop="templateCode"
                      :rules="[{ required: true, message: '请输入模板CODE', trigger: 'blur' }]"
        >
          <el-input v-model="sendForm.templateCode" placeholder="请输入模板CODE" />
        </el-form-item>
        <el-form-item label="消息类型" prop="msgType" :rules="[{ required: true, message: '请选择模板类型', trigger: 'blur' }]">
          <el-select v-model="sendForm.msgType" placeholder="请选择模板类型">
            <el-option label="短信" value="1" />
            <el-option label="邮件" value="2" />
            <el-option label="微信公众号" value="3" />
            <el-option label="站内信" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板参数" prop="testData">
          <el-input v-model="sendForm.testData" type="textarea" placeholder="请输入模板参数json" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSendMsg">
          确 定
        </el-button>
        <el-button @click="sendOpen=false">
          取 消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMsgTemplate,
  delMsgTemplate,
  getMsgTemplate,
  listMsgTemplate,
  updateMsgTemplate
} from "@/api/system/msgtemplate.js"
import {sendTemplateMsg} from "../../../api/system/msgtemplate"

export default {
  name: "MsgTemplate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // message表格数据
      msgtemplateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      sendOpen: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        templateName: null,
        templateCode: null,
        templateType: null,
        templateContent: null
      },
      // 问卷参数
      form: {},
      sendForm: {},
      // 问卷校验
      rules: {
        templateName: [
          {required: true, message: "模板名称不能为空", trigger: "blur"}
        ],
        templateCode: [
          {required: true, message: "模板CODE不能为空", trigger: "blur"}
        ],
        templateType: [
          {required: true, message: "模板类型不能为空", trigger: "change"}
        ],
        templateContent: [
          {required: true, message: "模板内容不能为空", trigger: "blur"}
        ],
        thirdTemplateId: [
          {required: true, message: "输入不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询message列表 */
    getList() {
      this.loading = true
      listMsgTemplate(this.queryParams).then((response) => {
        this.msgtemplateList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 问卷重置
    reset() {
      this.form = {
        id: null,
        templateName: null,
        templateCode: null,
        templateType: null,
        templateContent: null,
        thirdTemplateId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加消息模板"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMsgTemplate(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = "修改消息模板"
      })
    },
    /** 修改按钮操作 */
    handleOpenSend(row) {
      this.sendOpen = true
      this.sendForm.templateCode = row.templateCode
      this.sendForm.msgType = row.templateType
    },
    handleSendMsg() {
      this.$refs["sendForm"].validate((valid) => {
        if (valid) {
          sendTemplateMsg(this.sendForm).then((response) => {
            this.msgSuccess("发送完成")
            this.sendOpen = false
          })
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMsgTemplate(this.form).then((response) => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMsgTemplate(this.form).then((response) => {
              this.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除message编号为"' + ids + '"的数据项？').then(function () {
        return delMsgTemplate(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      }).catch(() => {
      })
    }
  }
}
</script>
